window.onload = function() {
	seajs.use(['jquery-1.8.2.js', 'iscroll_module.js', 'mdialog_module.js', 'util_module.js'], function(Z, I, M, U) {
		U.loadOpt(false);
		U.fillHead();
		U.fillFooter();
		var imagelist = new Array();
		var ids = new Array();
		var n=0;
		var _gmtCreate;
		//var newsDeList= new Array();
		var pageIndex=1;//公示榜定义分页页数
		var levelOneTitle=1;//公示榜定义一级标题点击
		var levelTwoTitle=1;//公示榜定义二级标题点击
		var leiji=0;//累计榜定义变量
		initPage();

		function initPage() { //初始化页面
			U.loadOpt(false);
			U.initScrollImg('bar', null, 2000);
			U.ajaxAsync("news/queryByType?newsTypeId=0&curPage=1&pageSize=10&contentType=2", {}, function(json) {
				$(".information_others >ul").empty();
				$(".bf-top >span").empty();
				if (json.success) {
					var newsDeList = json.models.newsDeList;
					//imagelist = json.models.imageList;
					//console.log(json);
					var item=0;
					$(newsDeList).each(function() {
						item+=1;
						if(item==1){
							/*$(".information_others >ul").append("<li class='other-title' id='" + this.id + "' title='"+this.title+"'><div style='height: 50px'><p class='p-title'>" + this.title + "</p></div><div style='height: 85px;margin-top: 12px'><p class='p-body'> " + this.title + "</p></div></li>");*/
							$(".bf-top .bf-title").attr({id:this.id,title:this.title});
							$(".bf-top .bf-title").css("cursor","pointer");
							$(".bf-top .bf-title").html(this.title);
							$(".bf-top .bf-content").html(this.content);
						}
						if(item>1 && item<10){
							$(".information_others >ul").append("<li class='other-li' id='" + this.id + "' title='"+this.title+"'><img src='../images/icons/icon_dian.png'><div>" + this.title + "</div></li>");
						}

					});
					var number=0;
					$(newsDeList).each(function(i) {
						if(newsDeList[i].images && number<4){
							number=number+1;
							var tem=$('.big_img').attr('src');
							if(!tem){
								$('.big_img').attr('src', newsDeList[i].images);
								$('.big_img').attr('data-id', newsDeList[i].id);
							}
							imagelist.push( newsDeList[i].images);
							ids.push( newsDeList[i].id);
							//$('.information_right').append('<div><img src="' + newsDeList[i].images + '" data-id="'+newsDeList[i].id+'" alt="加载中..." /></div>');
						}
						//$('.big_img').attr('src', newsDeList[i].images);
					});
					var list=0;
					if(ids.length>4){
						list=4;
					}else{
						list=ids.length;
					}
					for(var i=1;i<=list;i++){
						if(i==1){
							$(".bf-left div>ul").append('<li data-src="'+imagelist[i-1]+'" style="background-color:red" data-id="'+ids[i-1]+'" index="'+(i-1)+'">'+i+'</li>')
						}
						else{
							$(".bf-left div>ul").append('<li data-src="'+imagelist[i-1]+'" data-id="'+ids[i-1]+'" index="'+(i-1)+'">'+i+'</li>')
						}
					}

					$(".information_others li").click(function() {
						location.href = "Policy_details.html?id=" + $(this).attr('id');
					});

					$('.bf-left div>ul li').click(function(){
						//console.log(this.index);
						n=$(this).attr("index");
						$('.big_img').attr('src', $(this).attr('data-src'));
						$('.big_img').attr('data-id', $(this).attr('data-id'));
						$(".bf-left div>ul li").removeAttr("style");
						$(this).css("background-color","red");
					});



				} else {
					M.toast(false, json.errorMsg);
				}
			});
			var init=window.setInterval(function(){
				if (n > 3) n = 0; // 一共4张图片，所以循环替换
				$(".bf-left div>ul li").removeAttr("style");
				$(".bf-left div>ul li").eq(n).css("background-color","red");
				$('.big_img').attr('src', imagelist[n]).attr("data-id",ids[n]);
				n++;
			},3000);

			params = {
				curPage: 1,
				pageSize: 4,
				name: "",
				//typ: -1,
				//state: -1, //0(项目状态0-未完成,1-正在进行时,2-已完成)
				//provinceId: 1 //1(1-默认排序,2-时间排序)
			};
			U.ajaxAsync("/project/list", params, function(json) {
				$(".new_project").empty();
				U.loadOpt(false);
				if (json.success) {
					$(json.models.projectList).each(function(i) {
						if (i == 3) {
							return false;
						}
						var p_img = U.loadfail;
						if (U.getImg(this.images).length > 0) {
							p_img = U.getImg(this.images)[0];
						}
						var state = "未开展";
						if (this.state == 2) {
							state = "进行中";
						}
						if (this.state == 3) {
							state = "申请结束";
						}
						if (this.state == 4) {
							state = "已完成";
						}
						var project = "" +
							"<div id='" + this.id + "'> " +
								"<img class='project_img' src='" + p_img + "' />  " +
								"<div class='new_project_right'>" +
									"<div class='p_time'>" + U.getMyDate(this.gmtCreate) + "</div>" +
									"<div class='p_img'><img src='../images/icons/icon_gengfuo.png'></div>" +
								"</div>" +
								"<div class='new_project_content'>" +
									"<div class='p_title'><span>" + state + "</span></div>" +
									"<div class='p_name'>" + this.name + "</div>" +
									"<div class='p_content'>" + this.title + "</div>" +
								"</div>" +
							"</div>";
						$(".new_project").append(project);
					});
				} else {
					M.toast(false, json.errorMsg);
				}
			});
			U.loadOpt(true);
			/*专项帮扶对象列表*/
			var element = {
				curPage: 1,
				pageSize: 3,
				name: "",
				//typ: -1,
				//state: -1, //0(项目状态0-未完成,1-正在进行时,2-已完成)
				userId: -2
				//provinceId: 1 //1(1-默认排序,2-时间排序)
			};
			U.ajaxAsync("/poverty/list", element, function(json) {
				$(".dynamic").empty();
				U.loadOpt(false);
				if (json.success) {
					var povertyObjList = json.models.povertyObjList;
					nextPage = json.models.page.nextPage;
					$(povertyObjList).each(function(i) {
						if(i>2){
							return;
						}
						if(this.images == null || this.images == '') {
							this.images = '../images/icons/loadfail.png';
						}
						var project = "" +
							"<div id='" + this.id + "'> " +
							"<img class='project_img' src='" +this.images + "' />  " +
							"<div class='new_project_right'>" +
							"<div class='p_time'>" + U.getMyDate(this.gmtCreate) + "</div>" +
							"<div class='p_img'><img src='../images/icons/icon_gengfuo.png'></div>" +
							"</div>" +
							"<div class='new_project_content'>" +
							"<div class='p_title'><span>"+ this.name +"</span></div>" +
							"<div class='p_name'>地址：" + this.address + "</div>" +
							"<div class='p_content'><span style='width:320px;height: 20px;display: block'>帮扶需求：" + (this.requirement==null?"":this.requirement) + "</span></div>" +
							"</div>" +
							"</div>";
						$(".dynamic").append(project);
					});
				} else {
					M.toast(false, json.errorMsg);
				}
			});
			/*U.ajaxAsync("/crowd/specialList", {}, function(json) {
				$(".dynamic").empty();
				U.loadOpt(false);
				if (json.success) {
					var adv ="<script type='text/javascript' src='http://cdn.gbtags.com/jquery/2.1.1/jquery.min.js'></script><script  src='js/jquery.progress.js'></script>" ;
					$(json.models.specialList).each(function(i) {
						if (i > 2) {
							return false;
						}
						var dynameic_img = this.images;
						_gmtCreate = U.getMyDate(this.gmtCreate);
						//console.log(this.progress.substring(0,this.progress.length-1));
						var percentage=this.progress.substring(0,this.progress.length-1);
						var day=this.residualTime.substring(0,this.residualTime.length-1);
						 adv = adv+"" +
							 "<div id='"+this.id+"'>" +
							 	"<div class='d_left'>" +
									"<img src='" + dynameic_img + "'>" +
								"</div>" +
								"<div class='d_right'>" +
										"<div class='crowd-top'>" +
											"<span class='p_name' data-value='"+this.title+"'>"+this.title+"</span>" +
										"</div>" +
										"<div class='crowd-center'>" +
												 "<div class='progress'>" +
														"<svg class='container' data-value='"+percentage+"'></svg>" +
							 							"<div style='display: none' class='intro' data-value='"+this.intro+"' ></div>" +
												 "</div>" +
										 "</div>" +
										"<div class='crowd-bottom'>" +
												 "<div class='crowd-bottom-item'>" +
														 "<span class='sp1'>"+(this.state==3?"已完成":this.progress)+"</span>" +
														 "<span>已达</span>" +
												 "</div>" +
												 "<div class='crowd-bottom-item'>" +
														 "<span class='sp2 totalAmount' data-value='"+(this.totalAmount/100)+"'>"+(this.totalAmount/100)+"元</span>" +
														 "<span>已筹款</span>" +
												 "</div>" +
												 "<div class='crowd-bottom-item'>" +
														 "<span class='sp1 residualTime' data-value='"+this.residualTime+"'>"+(this.state==3?"已完成":this.residualTime)+"</span>" +
														 "<span>剩余时间</span>" +
												 "</div>" +
										 "</div>" +
								 "</div>" +
							"</div>";
					});
					$(".dynamic").append(adv);
					var sr="<script> $('.container').each(function(){var progress = $(this).Progress({percent: $(this).attr('data-value'),width: 400,height:10,fontSize: 0});}); </script>";

					$(".dynamic").append(sr);


				} else {
					M.toast(false, json.errorMsg);

				}
			});*/
			U.loadOpt(true);
			U.ajaxAsync("/message/queryByVisi", {
				"visible": "1","pageSize":7
			}, function(json) {
				$(".Notice_others>ul").empty();
				U.loadOpt(false);
				if (json.success) {
					$(json.models.messageList).each(function() {
						$(".Notice_others>ul").append("<li id='" + this.id + "' title='" + this.title + "'><img src='../images/icons/icon_dian.png'><div>" + this.title + "</div></li>");
					});
				} else {
					M.toast(false, json.errorMsg);
				}
			});
			//$( "#progressbar" ).append("<div>"+$(this).attr("value")+"</div>");
			initEvent();
		}
		/*捐款榜*/
		function get_donateList1() {
			$('.donate_list1').html('');
			var donate_type = {
				'typ': 1,
				'state':2
			};
			var param = {
				'pageSize': 10,
				'currPage': 1,
				'qcommonStr': JSON.stringify(donate_type)
			};
			U.ajax('donation/list', param, function(json) {
				if (json.success) {
					var result = json.models.donationList;
					$(result).each(function() {
						$('.donate_list1').append('<div class="donate"><ul><li>' + U.getMyDate(this.gmtCreate) + '</li><li>' + this.payer + '</li><li>￥' + this.amount / 100.0 + '</li></ul></div>');
					});
				} else {
					M.toast(false, json.errorMsg);
				}
			});
		}
		/*捐款榜*/
		function get_donateList2() {
			$('.donate_list2').html('');
			var donate_type = {
				'typ': 2,
				'state':2
			}
			var param = {
				'pageSize': 10,
				'currPage': 1,
				'qcommonStr': JSON.stringify(donate_type)
			}
			U.ajax('donation/list', param, function(json) {
				if (json.success) {
					var result = json.models.donationList;
					$(result).each(function() {
						/*if(this.payer.length>7){
							this.payer=U.formPhone(this.payer);
						}*/
						$('.donate_list2').append('<div class="donate"><ul><li>' + U.getMyDate(this.gmtCreate) + '</li><li>' + this.payer + '</li><li>￥' + this.amount / 100.0 + '</li></ul></div>');
					});
				} else {
					M.toast(false, json.errorMsg);
				}
			});
		}
		/*结对帮扶公示*/
		function tearHelp(type){
			var param = {
				'state':-2,
				'curPage':pageIndex++
			};
			if(type==2){
				param = {
					'state':4,
					'curPage':pageIndex++
				}
			}
			U.ajax('fplogs/listAll', param, function(json) {
				if (json.success) {
					var result = json.models. fpLogsList;
					/*获取当前的总页数*/
					$('.donate_list').attr("totalPage",json.models.page.totalPage);
					$(result).each(function () {
						var p_img = U.loadicon;
						if(U.getImg(this.images).length > 0) {
							p_img = U.getImg(this.images)[0];
						}
						if(type==2){
							$('.donate_list').append('<div class="donate"><div class="tearHelp tearleft tearlast"><img src='+p_img+'><span title="'+this.projectName+'">'+this.projectName+'</span></div><div   class="tearHelp tearright"><span>' + U.getMyDate(this.gmtCreate) + '</span></div></div>');
						}else {
							$('.donate_list').append('<div class="donate"><div class="tearHelp tearleft"><span class="sp-tear sp-conter" title="'+this.content+'">'+this.content+'</span></div><div class="tearHelp tearright"><span>' + U.getMyDate(this.gmtCreate) + '</span></div></div>');
						}

					});
					//console.log($('.donate_list').scrollTop("123"));
				} else {
					M.toast(false, json.errorMsg);
				}
			})
		}
		/*专项帮扶*/
		function specialHelp(type){
			if(type==1){
				U.ajax('donation/donationPublicList', {"curPage":pageIndex++,userId:-1}, function(json) {
					if (json.success) {
						/*获取当前的总页数*/
						$('.donate_list').attr("totalPage",json.models.page.totalPage);
						var result = json.models.donationList;
						$(result).each(function () {
							$('.donate_list').append('<div class="donate"><div class="specia-left special""><span title="'+this.content+'">'+this.content+'</span></div><div class="specia-right"><span>' + U.getMyDate(this.gmtCreate) + '</span></div></div>');
						});
					} else {
						M.toast(false, json.errorMsg);
					}
				})
			}
			else{
				U.ajaxget('crowd/giveMoneyList',  {"curPage":pageIndex++}, function(json) {
					if (json.success) {
						var result = json.models.crowdList;
						$('.donate_list').attr("totalPage",json.models.page.totalPage);
						$(result).each(function () {
							$('.donate_list').append('<div class="donate"><div class="specia-left" title="专项帮扶项目：'+this.title+'"><span>专项帮扶项目：'+this.title+'</span><span title="'+(this.billType==1?"基金会":"协会")+'支持了'+(this.billAmount/100)+'元">'+(this.billType==1?"基金会":"协会")+'支持了'+(this.billAmount/100)+'元</span></div><div class="specia-right"><span>' + U.getMyDate(this.gmtModify) + '</span></div></div>');
						});
					} else {
						M.toast(false, json.errorMsg);
					}
				})
			}

		}
		/*基金会帮扶*/
		function fundHelp(type){
			if(type==1){
				U.ajax('donation/donationPublicList',  {"curPage":pageIndex++}, function(json) {
					if (json.success) {
						/*获取当前的总页数*/
						$('.donate_list').attr("totalPage",json.models.page.totalPage);
						var result = json.models.donationList;
						$(result).each(function () {
							$('.donate_list').append('<div class="donate"><div class="tearHelp tearleft tearlast funhelp"><img src="../images/icons/qq.png"><span title="'+this.content+'">'+this.content+'</span></div><div  class="tearHelp tearright"><span>' + U.getMyDate(this.gmtCreate) + '</span></div></div>');
						});
					} else {
						M.toast(false, json.errorMsg);
					}
				})
			}
			else if(type==2){
				U.ajaxget('donation/donationPersonTotal',  {"curPage":pageIndex++}, function(json) {
					if (json.success) {
						$('.donate_list').attr("totalPage",json.models.page.totalPage);
						var result = json.models.donationList;
						$(result).each(function (i) {
							var doc="";
							if(leiji==0){
								doc='<img src="../images/icons/number1.png" >'
							}else if(leiji==1){
								doc='<img src="../images/icons/number2.png" >'
							}else if(leiji==2){
								doc='<img src="../images/icons/number3.png" >'
							}else {
								doc='<div style="width: 30px;height: 30px;border: 1px solid #959595;border-radius: 50%;float: left;margin-top: 14px;font-size: 20px;text-align: center">'+(leiji+1)+'</div>';
							}
							leiji=leiji+1;
							$('.donate_list').append('<div class="donate persondonate"><div class="tearHelp tearleft tearlast leijibang">'+doc+'<span class="sp-ck" data-userid="'+this.userId+'" style="cursor: pointer" title="'+this.payer+'支持了'+(this.amount/100)+'元">'+this.payer+'支持了'+(this.amount/100)+'元</span></div></div>');




						});
						$(".sp-ck").click(function(){
							$(".donate_list").empty();
							U.ajax('donation/donationPublicList', {userId:$(this).attr("data-userid"),'curPage':100}, function(json) {
								if (json.success) {
									var result = json.models.donationList;
									$(result).each(function () {
										$('.donate_list').append('<div class="donate"><div class="tearHelp tearleft tearlast"><img src="../images/icons/qq.png"><span title="'+this.content+'">'+this.content+'</span></div><div  class="tearHelp tearright"><span>' + U.getMyDate(this.gmtCreate) + '</span></div></div>');
									});
								} else {
									M.toast(false, json.errorMsg);
								}
							})
							/*alert(123);
							console.log($(this).html());*/
						})
					} else {
						M.toast(false, json.errorMsg);
					}
				})
			}
			else{
				var param={
					'billType':1,
					'curPage':pageIndex++
				}
				U.ajaxget('crowd/giveMoneyList', param, function(json) {
					if (json.success) {
						$('.donate_list').attr("totalPage",json.models.page.totalPage);
						var result = json.models.crowdList;
						$(result).each(function () {
							$('.donate_list').append('<div class="donate"><div class="specia-left give"><span title="专项帮扶项目：'+this.title+'">专项帮扶项目：'+this.title+'</span><span title="'+(this.billType==1?"基金会":"协会")+'发放了'+(this.billAmount/100)+'元">'+(this.billType==1?"基金会":"协会")+'发放了'+(this.billAmount/100)+'元</span></div><div class="specia-right"><span>' + U.getMyDate(this.gmtModify) + '</span></div></div>');
						});
					} else {
						M.toast(false, json.errorMsg);
					}
				})
			}
		}
		/*加载数据*/
		function initEvent() {

			$('.information_right>div img').click(function() {

			});
			$('.big_img').click(function(){
				location.href = "Policy_details.html?id=" + $(this).attr("data-id");
			});

			$('.donate_type li').click(function() {
				/*$('.donate_list1,.donate_list2').hide();*/
				$('.donate_type li').css({
					'background': '#F0F5F9',
					'color': 'black'
				});
				$(this).css({
					'background': 'red',
					'color': 'white'
				});
				if ($(this).attr('type') ==1) {
					levelOneTitle=1;//表示为结对帮
					$(".intro_title").empty();
					$(".intro_title").append("<ul class='tear-help'><li type='1'>帮扶记录</li></ul>");//<li type='2'>帮扶成果</li>
					$(".intro_title li").click(function(){
						$(".donate_list").empty();//清空列表
						pageIndex=1;//重定义分页
						$(this).toggleClass("select-li").siblings(".select-li").removeClass("select-li");
						tearHelp($(this).attr("type"));
					})
				}
				if ($(this).attr('type') ==2) {
					levelOneTitle=2;//表示专项榜
					$(".intro_title").empty();
					$(".intro_title").append("<ul class='tear-help'><li type='1'>帮扶记录</li></ul>");//<li type='2'>帮扶成果</li>
					$(".intro_title li").click(function(){
						$(".donate_list").empty();//清空列表
						pageIndex=1;//重定义分页
						$(this).toggleClass("select-li").siblings(".select-li").removeClass("select-li");
						specialHelp($(this).attr("type"));
					})
				}
				if ($(this).attr('type') ==3) {
					levelOneTitle=3;//表示捐款榜
					$(".intro_title").empty();
					$(".intro_title").append("<ul class='jijin-help'><li type='1'>即时榜</li><li type='2'>累计榜</li><li type='3'>帮扶明细</li></ul>");
					$(".intro_title li").click(function(){
						$(".donate_list").empty();//清空列表
						pageIndex=1;//重定义分页
						leiji=0;
						$(this).toggleClass("select-li").siblings(".select-li").removeClass("select-li");
						levelTwoTitle=$(this).attr("type");
						fundHelp($(this).attr("type"));
					})

				}
				$(".intro_title li").eq(0).click();
			});

			$(".donate_type li").eq(0).click();
			$(".Notice_others >ul>li").click(function() {
				location.href = "announcement_details.html?id=" + $(this).attr("id");
			});
			$(".other_function>div").click(function() {
				var page = "";
				switch ($(this).index()) {
					case 0:
						page = "apply.html";
						break;
					case 1:
						var user = U.checkLogin();
						if (user.state != 1) {
							location.href = "apply.html";
							return;
						}
						if (user.info.typ == 0) {
							page = "apply.html";
						} else {
							page = "search_result.html";
						}
						break;
					case 2:
						page = "help_entrance.html?accountType="+ 1;
						break;
				}
				location.href = page;
			});
			$(".Notice_more").click(function() {
				location.href = "notice_list.html"
			});
			$(".more_zx").click(function() {
				location.href = "policy_list.html?onlynews=1";
			});
			$(".information_others li").click(function() {
				location.href = "Policy_details.html?id=" + $(this).attr("id");
			});
			$(".bf-title").click(function() {
				location.href = "Policy_details.html?id=" + $(this).attr("id");
			});
			$(".more_project").click(function() {
				location.href = "help_project.html";
			});
			//专项帮扶对象列表
			$(".more_dynamic").click(function() {
				//location.href = "help_special.html";
				location.href = "search_result.html?specialId=-2";
			});
			/*$(".dynamic>div").unbind("click").click(function() {
				location.href = "dynamic.html";
			});*/
			$(".new_project>div").click(function() {
				location.href = "project_details.html?id=" + $(this).attr("id");
			});
			$(".dynamic>div").click(function() {
				//console.log(_gmtCreate);
				/*console.log($(this).attr("id"));
				console.log($(this).find(".p_name").attr("data-value"));
				console.log($(this).find("svg").attr("data-value"));
				console.log($(this).find(".totalAmount").attr("data-value"));
				console.log($(this).find(".residualTime").attr("data-value"));
				console.log($(this).find("img").attr("src"));
				console.log($(this).find(".intro").attr("data-value"));
				return false;*/
				/*location.href = "special_details.html?id=" + $(this).attr("id")+"&title="+$(this).find(".p_name").attr("data-value")+"&progress="+$(this).find("svg").attr("data-value")+"&totalAmount="+$(this).find(".totalAmount").attr("data-value")+"&residualTime="+$(this).find(".residualTime").attr("data-value")+"&images="+$(this).find("img").attr("src")+"&intro="+$(this).find(".intro").attr("data-value")+"&gmtCreate="+_gmtCreate;*/
				location.href = 'poor_peoleDetail.html?specialId=-2&id='+$(this).attr("id");
			});
			$('.list>p').click(function() {
				location.href = 'search_result.html';
			});
			U.progress($(".progress"));



			$(".donate_list").scroll(function(type){
				var totalPage=$(this).attr("totalPage");
				var $this =$(this),
					viewH =$(this).height(),//可见高度
					contentH =$(this).get(0).scrollHeight,//内容高度
					scrollTop =$(this).scrollTop();//滚动高度
				//if(contentH - viewH - scrollTop <= 100) { //到达底部100px时,加载新内容
				if(scrollTop/(contentH -viewH)>=1){ //到达底部100px时,加载新内容
					// 这里加载数据..
					if(pageIndex<=totalPage){
						if(levelOneTitle==1){
							tearHelp(1);
						}
						else if(levelOneTitle==2){
							specialHelp(1);
						}
						else{
							fundHelp(levelTwoTitle);
						}
					}
				}
			});

			/*var nScrollHight = 0; //滚动距离总长(注意不是滚动条的长度)
			var nScrollTop = 0;   //滚动到的当前位置
			var nDivHight = $("#div1").height();
			$("#div1").scroll(function(){
				nScrollHight = $(this)[0].scrollHeight;
				nScrollTop = $(this)[0].scrollTop;
				if(nScrollTop + nDivHight >= nScrollHight)
					alert("滚动条到底部了");
			});*/

		}
	});
};